/**
 * 
 */
package com.base.myproject.client.busi.system;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.Record;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author zgw 2009-11-1 下午08:48:00 javazgw@gmail.com
 */
public class MenuPermissionsX extends BaseJDBCPanel {
	BaseStore<BusiObject> groupstore = null;
	BaseStore<BusiObject> menustore = null;

	public MenuPermissionsX() {
		super();
		init();
	}

	public void init() {

		
		CreateGroupStore();
		CreateMenuStore();
		this.setWidth("960");
		// this.setHeight("720px");
		this.setAutoHeight(true);

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWeigetx(), centerData);

		// registerMainStore(store);
		// registerStore(store);

		this.setBusino(busi);
		setdefault();

	}

	private void setdefault() {
		/*
		 * if(store!=null) { store.addDefault("busicode", busi);
		 * store.addDefault("bcode", Env.getEnvbcode()); }
		 */
	}

	public Widget initWeigetx() {
		// LayoutContainer lc1 = new LayoutContainer();
		HorizontalPanel hpanel = new HorizontalPanel();

		hpanel.add(CreateGroupGrid());
		hpanel.add(CreateMenuGrid());
		// hpanel.setWidth("960");

		return hpanel;
	}

	
	void CreateMenuStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("menucode");
		type.addField("menuname");
		type.addField("isset");
		type.getField("isset").setType(Boolean.class);
		// type.addField("Phone");
		// type.addField("imagepath");
		StoreInfo info = new StoreInfo();
		info.setTablename("menu");
		
		String sql = "select a.menucode,a.menuname,b.groupicode, ";
		sql +=		" (case when (b.groupicode is null) then '0' else '1' end )as isset ";
		sql +=	"  from  ";
		sql +=	" (";
		sql +=	" select * from menu "; 
		sql +=	" ) as a ";
		sql +=	" left join "; 
		sql +=	" ( ";
		sql +=	" select * from menupermissions  where groupicode ='{groupicode}' ";
		sql +=	" ) as b ";
		sql +=	" on a.menucode = b.menucode order by a.menucode";
		
		//info.setSql("select *,1 as isset from menu order by menucode");
		info.setSql(sql);
		info.setKeyname(new String[] { "icode" });
		info.setLinkparentcolumns(new String[] { "groupicode" }); // 关联主表字段
		//info.setLinkparent_chile_columns(new String[] { "groupicode" });// 关联主表字段// (子表)
		info.setNeedAllRow("no");	
		info.setParent(groupstore);
		
		menustore = new BaseStore<BusiObject>(type, info);
		//menustore.setParent(groupstore);
		

//		menustore.setLinkparentcolumns(new String[]{"groupicode"});
//		menustore.setLinkparent_chile_columns(new String[]{"groupicode"});

	}

	void CreateGroupStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.addField("icode");
		type.addField("groupicode");
		type.addField("groupname");
		// type.addField("Phone");
		// type.addField("imagepath");
		StoreInfo info = new StoreInfo();
		info.setTablename("basegroup");
		info.setSql("select * from basegroup order by groupicode");
		info.setKeyname(new String[] { "icode" });
		info.setNeedAllRow("no");
		groupstore = new BaseStore<BusiObject>(type, info);

	}

	BaseGrid<BusiObject> CreateGroupGrid() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig cc1 = new ColumnConfig("groupicode", "组号", 100);
		// TextField<String> menucodetext = new TextField<String>();
		// menucode.setEditor(new CellEditor(menucodetext));
		columns.add(cc1);

		ColumnConfig cc2 = new ColumnConfig("groupname", "组名", 100);
		// TextField<String> cctext2 = new TextField<String>();
		// cc2.setEditor(new CellEditor(cctext2));
		columns.add(cc2);

		ColumnModel cm = new ColumnModel(columns);
		BaseGrid<BusiObject> grid = new BaseGrid<BusiObject>(groupstore, cm,
				false, false, null);
		// grid.setAutoHeight(true);
		// grid.setAutoWidth(true);
		grid.setStripeRows(true);
		// grid.setBorders(true);
//		grid.setHeight(SystemMessage.fullheightinmain);
//		grid.setWidth(SystemMessage.fullwidthinmain);
		// grid.setStyleAttribute("borderTop", "none");
//		grid.setAutoWidth(true);
		grid.setSize(300, 450);
		grid.setSelectionModel(new GridSelectionModel<BusiObject>());
		return grid;
	}

	BaseGrid<BusiObject> CreateMenuGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		cbs.setSelectionMode(SelectionMode.MULTI);
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		// columns.add(cbs.getColumn());
		ColumnConfig menucode = new ColumnConfig("menucode", "菜单号", 100);
		TextField<String> menucodetext = new TextField<String>();
		menucode.setEditor(new CellEditor(menucodetext));
		columns.add(menucode);

		ColumnConfig cc2 = new ColumnConfig("menuname", "菜单名", 150);
		TextField<String> cctext2 = new TextField<String>();
		cc2.setEditor(new CellEditor(cctext2));
		columns.add(cc2);

		CheckColumnConfig checkColumn = new CheckColumnConfig("isset", "分配?",80);
		CellEditor checkBoxEditor = new CellEditor(new CheckBox());
		checkColumn.setEditor(checkBoxEditor);
		columns.add(checkColumn);

		ColumnModel cm = new ColumnModel(columns);
		BaseGrid<BusiObject> grid = new BaseGrid<BusiObject>(menustore, cm,
				false, false, null);
		// grid.setAutoHeight(true);
		// grid.setAutoWidth(true);
		grid.setStripeRows(true);
		// grid.setBorders(true);
//		grid.setHeight(SystemMessage.fullheightinmain);
//		grid.setWidth(SystemMessage.fullwidthinmain);
	//	grid.setAutoWidth(true);
		// grid.setStyleAttribute("borderTop", "none");
		// grid.setSelectionModel(new GridSelectionModel<BusiObject>());
		grid.addPlugin(checkColumn);
		grid.setSize(400, 450);
		// ---

		// checkColumn.addListener(Events., listener)
		return grid;
	}

	public void initToolBarItems() {
		Button save = new Button("保存");
		save.setIconStyle("disk_black");
		// tti.setIconStyle(".my-icon {background-image: url(public/baseimage/converticon.com__DB_Save16.PNG) no-repeat center left !important; }");
		// sava.setIconStyle("my-saveicon");
		save.addSelectionListener(new SelectionListener() {
			public void componentSelected(ComponentEvent ce) {
				save();
			}
		});
		toolbarll.add(save);
		initToolBar();
	}

	protected void save() {
		Debug.printf(menustore.getModifiedRecords().size(), 4);
		Debug.printf(menustore.getModifiedRecords().get(0).get("menucode"), 4);
		Object groupid = groupstore.getValue("groupicode");
		java.util.Vector<String> v = new java.util.Vector<String>();
		for (Record r : menustore.getModifiedRecords()) {
			Debug.printf(r.get("menucode"), 4);
			String sql = "";
			if (r.get("isset").equals(Boolean.TRUE)) {
				Debug.printf(r.get("menucode") + "  set", 4);
				sql = "insert into menupermissions (menucode,groupicode) values('"
						+ r.get("menucode") + "','" + groupid + "') ";

			} else {
				Debug.printf(r.get("menucode") + " cancel set", 4);
				sql = " delete menupermissions where menucode='"
						+ r.get("menucode") + "' and groupicode='" + groupid
						+ "'";
			}
			v.add(sql);
			Debug.printf(sql, 4);
		}

		SendServlet ss = new SendServlet();
		ss.ExcJDBCSQL(v, new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				Info.display("", "保存失败");

			}

			@Override
			public void onSuccess(String result) {
				if (result.equals(SystemMessage.getMessage(4, 0))) {
					Info.display("", "成功保存");
					menustore.commitChanges();
				} else {
					Info.display("", "保存失败");
				}
			}

		});

	}
}
